package com.xm9m.web.service.interfaces;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Liutong on 12/17/15.
 */
public abstract class AbstractRestApi implements IRestApi {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRestApi.class);

    protected HttpServletRequest httpServletRequest;

    /**
     *签名
     */
    protected String sign;


    /**
     * 时间戳
     */
    protected Long st;

    /**
     * 设备ID
     */
    protected String deviceId;

    protected Long userId;

    /**
     * Josn转Bean
     * @param data
     * @param clazz
     * @param <T>
     * @return
     */
    public <T extends IXm9mRequest> T json2Bean(String data,Class<T> clazz){
        try {
            return JSON.parseObject(data,clazz);
        } catch (JSONException e){
            LOGGER.error("Json parse object error! json:"+data,e);
        }

        return null;
    }

    @Override
    public void setHttpServletRequest(HttpServletRequest request) {
        this.httpServletRequest = request;
    }

    @Override
    public HttpServletRequest getHttpServletRequest() {
        return this.httpServletRequest;
    }


    @Override
    public String getSign() {
        return sign;
    }

    @Override
    public void setSign(String sign) {
        this.sign = sign;
    }

    @Override
    public Long getSt() {
        return st;
    }

    @Override
    public void setSt(Long st) {
        this.st = st;
    }

    @Override
    public String getDeviceId() {
        return deviceId;
    }

    @Override
    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    @Override
    public Long getUserId() {
        return userId;
    }

    @Override
    public void setUserId(Long userId) {
        this.userId = userId;
    }
}
